use aes::Aes128;
use cfb_mode::Cfb;
use cfb_mode::cipher::{NewStreamCipher, StreamCipher};

type AesCfb = Cfb<Aes128>;

pub fn encrypt(plaindata: &Vec<u8>, key: &[u8], iv: &[u8]) -> Vec<u8> {
    let mut result = plaindata.clone();
    AesCfb::new_var(key, iv).unwrap().encrypt(&mut result);
    result
}

pub fn decrypt(cipherdata: &Vec<u8>, key: &[u8], iv: &[u8]) -> Vec<u8> {
    let mut result = cipherdata.clone();
    AesCfb::new_var(key, iv).unwrap().decrypt(&mut result);
    result
}

#[cfg(test)]
mod tests {
    use hex_literal::hex;
    #[test]
    fn it_works() {
        let key = hex!("013F65A3AE564CA02D5215C36D71DBB3");
        let iv = hex!("013F6543AE564CA01D5215C36D71DBB3");

        let plaindata = b"test aes cfb";

        let cipherdata = super::encrypt(&plaindata.to_vec(), &key, &iv);
        let plaindata2 = super::decrypt(&cipherdata, &key, &iv);
        println!("{:?}", cipherdata);
        println!("{:?}", plaindata2);
        assert_eq!(cipherdata.len(), plaindata.len());
        assert_eq!(plaindata.to_vec() , plaindata2);
    }
}